<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<button type="button">点击</button>
		<script type="text/javascript">
			//this 指向问题,一般情况下this最终指向的是那个调用它的对象
			//1.全局作用域或者普通函数中this指向全局对象window(注意定时器里面的this也是指向window)
			console.log(this);

			function fn() {
				console.log(this);
			}

			window.fn();

			window.setTimeout(function() {
				console.log(this);
			}, 1000)

			//2.方法调用调用this指向谁
			var o = {
				sayHi: function() {
					console.log(this); //this指向o对象
				}
			}
			o.sayHi();

			var btn = document.querySelector("button");
			btn.onclick = function() {
				console.log("btn1:" + this);
			}

			btn.addEventListener("click", function() {
				console.log("btn2:" + this); //指向btn对象
			})

			//3.构造函数中this指向构造函数的实例
			function Fun() {
				console.log(this);
			}

			var fun = new Fun();
		</script>
	</body>
</html>
